package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.ecologypool.EcologyPoolRedPackAuditRequest;
import com.sc.nft.admin.request.ecologypool.EcologyPoolRedPackDataRequest;
import com.sc.nft.admin.request.ecologypool.EcologyPoolRedPackPageRequest;
import com.sc.nft.admin.request.ecologypool.EcologyPoolRedPackStatusRequest;
import com.sc.nft.entity.vo.EcologyPoolRedPackReceiveVO;
import com.sc.nft.entity.vo.ecologypool.EcologyPoolDaoCommunityServiceRankIncomePrizeVo;
import com.sc.nft.entity.vo.ecologypool.EcologyPoolRedPackDataDetailVO;
import com.sc.nft.entity.vo.ecologypool.EcologyPoolRedPackDetailVO;
import com.sc.nft.entity.vo.ecologypool.EcologyPoolRedPackPageVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologyPoolRedPackService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/redPack")
public class EcologyPoolRedPackController {
    private final Redisson redisson;
    private final EcologyPoolRedPackService ecologyPoolRedPackService;


    @ApiOperation(value = "奖励类型列表", notes = "奖励类型列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoCommunityServiceRankIncomePrizeTypeList", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<EcologyPoolDaoCommunityServiceRankIncomePrizeVo>> daoCommunityServiceRankIncomePrizeTypeList() {
        return Result.success(ecologyPoolRedPackService.daoCommunityServiceRankIncomePrizes());
    }

    @ApiOperation(value = "红包活动列表", notes = "红包活动列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<EcologyPoolRedPackPageVO> daoRedPackPage(@RequestBody EcologyPoolRedPackPageRequest request) {
        return PageResult.success(ecologyPoolRedPackService.daoRedPackPage(request.getDaoId(), request.getPageNo(), request.getPageSize(), request.getName(), request.getAuditStatus()));
    }

    @ApiOperation(value = "红包详情", notes = "红包详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackPageDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<EcologyPoolRedPackDetailVO> redPackPageDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolRedPackService.redPackPageDetail(request.getId()));
    }

    @ApiOperation(value = "红包数据详情-分页", notes = "红包数据详情-分页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackDataPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<EcologyPoolRedPackReceiveVO> redPackDataPage(@RequestBody EcologyPoolRedPackDataRequest request) {
        return PageResult.success(ecologyPoolRedPackService.redPackDataPage(request.getPageNo(), request.getPageSize(), request.getId(), request.getUserTel()));
    }


    @ApiOperation(value = "数据详情-红包总额数据", notes = "数据详情-红包总额数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackDataDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<EcologyPoolRedPackDataDetailVO> redPackDataDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolRedPackService.redPackDataDetail(request.getId()));
    }


    @ApiOperation(value = "开启关闭活动", notes = "开启关闭活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackOpen", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Boolean> redPackOpen(@RequestBody @Valid EcologyPoolRedPackStatusRequest request) {
        ecologyPoolRedPackService.redPackOpen(request.getId(), request.getStatus());
        return Result.success();
    }


    @ApiOperation(value = "活动审核", notes = "活动审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/auditRedPack", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Boolean> auditRedPack(@RequestBody @Valid EcologyPoolRedPackAuditRequest request) {
        request.checkRejectReason();
        RLock lock = redisson.getLock(String.format(RedisKeyEnum.AUDIT_RED_PACK_LOCK.getKey(), request.getId()));
        try {
            boolean res = lock.tryLock(10, TimeUnit.SECONDS);
            if (!res) {
                throw new GlobalRunTimeException("太火爆了,请稍后再试");
            }
            ecologyPoolRedPackService.auditRedPack(request.getId(), request.getAuditStatus(), request.getRejectReason());
        } catch (GlobalRunTimeException e) {
            log.error("新增红包异常", e);
            throw e;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
        return Result.success();
    }


//    @GetMapping(value = "/redPackOnSiteInspection")
//    public Result<Boolean> redPackOnSiteInspection() {
//        new Thread(ecologyPoolRedPackService::redPackOnSiteInspection).start();
//        return Result.success();
//
//    }
//
//    @GetMapping(value = "/redPackExpiredPointsRefund")
//    public Result<Boolean> redPackExpiredPointsRefund() {
//        new Thread(ecologyPoolRedPackService::redPackExpiredPointsRefund).start();
//        return Result.success();
//    }
}